package com.example.uniapp.dao;

import android.annotation.SuppressLint;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.example.uniapp.bean.CookbookFoodBean;
import com.example.uniapp.bean.CookbookStepBean;
import com.example.uniapp.db.DBUtils;

import java.util.ArrayList;
import java.util.List;

public class CookbookStepDao {

    public static SQLiteDatabase db = DBUtils.con;

    /**
     * 根据食谱id获取食谱制作步骤
     * @param cookbookId
     * @return
     */
    public static List<CookbookStepBean> getCookbookStepByCookbookId(String cookbookId) {
        List<CookbookStepBean> cookbookStepBeanList = new ArrayList<>();
        Cursor cursor = db.rawQuery("select * from cookbookstep where cookbook_id=?", new String[]{cookbookId});
        while (cursor.moveToNext()) {
            @SuppressLint("Range") String cookbookStep = cursor.getString(cursor.getColumnIndex("cookbook_step"));
            CookbookStepBean cookbookStepBean = new CookbookStepBean(cookbookId, cookbookStep);
            cookbookStepBeanList.add(cookbookStepBean);
        }
        return cookbookStepBeanList;
    }

    /**
     * 添加食谱步骤
     * @param cookbookId
     * @param cookbookStep
     * @return
     */
    public static int addCookbookStep(String cookbookId, String cookbookStep) {
        String[] data = {cookbookId, cookbookStep};
        try {
            db.execSQL("INSERT INTO cookbookstep (cookbook_id, cookbook_step)" +
                            "VALUES(?, ?)",
                    data);
            return 1;
        }catch (Exception e) {
            return 0;
        }
    }

    /**
     * 根据食谱id删除相应的食谱步骤
     * @param cookbookId
     * @return
     */
    public static int deleteCookbookStep(String cookbookId) {
        try {
            db.execSQL("delete from cookbookstep where cookbook_id=?", new String[] {cookbookId});
            return 1;
        }catch (Exception e) {
            return 0;
        }
    }
}
